'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFGckCtrl
 * @description
 * # FinanceFGckCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFGckCtrl', function($scope, $rootScope, openApi, $http, Util, $location, $routeParams, localStorageService, FormSubmit, TaskReviewService) {
        var params, selectData = {};
        // 底部款项类型选框添加
        var hasInvoiceArr = [{
            key: "已提供",
            value: "已提供"
        }, {
            key: "未提供",
            value: "未提供"
        }];
        var relModuleObj = {}
        var fapiao = {};
        fapiao.sheettitle = "缴税发票";
        fapiao.tabList = hasInvoiceArr;
        $scope.sheetObj = {};
        $scope.sheetObj.fapiao = fapiao
        $scope.sheetObj.fapiao.callback = function(item) {
            $scope.param.hasInvoice = item.selectTab
            if ($scope.param.hasInvoice == "未提供") {
                $scope.param.taxReceiptNo = "";
                $scope.param.taxReceiptNo2 = "";
                $scope.param.taxReceiptNo3 = "";
            }
            $scope.param.hasInvoiceContent = item.selectTabContent

        }
        $scope.showSheet = function(type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        // 工程款大类
        var gckTypeObj = {};
        gckTypeObj.sheettitle = "工程款类型(大类)";
        gckTypeObj.callback = function(item) {
            $scope.param.payType1 = item.selectTab
            $scope.param.payType2 = ""
        }
        $scope.sheetObj.gckTypeObj = gckTypeObj;
        $scope.showgckTypeObj = function() {
            var param = {};
            param.type = "工程款类型";
            Util.getKeyValue(param, function(data) {
                $scope.sheetObj.gckTypeObj.tabList = data.data
                $scope.showSheet("gckTypeObj")
            })

        };
        // 工程款小类
        var gckTypeObjA = {};
        gckTypeObjA.sheettitle = "工程款类型(小类)";
        gckTypeObjA.callback = function(item) {
            $scope.param.payType2 = item.selectTab
        }
        $scope.sheetObj.gckTypeObjA = gckTypeObjA;
        $scope.showgckTypeObjA = function() {
            if(!$scope.param.payType1){
                Util.Toast("请先选择工程款类型大类");
                return
            }
            var param = {};
            param.type = "工程款类型";
            param.parent = $scope.param.payType1;
            Util.getKeyValue(param, function(data) {
                $scope.sheetObj.gckTypeObjA.tabList = data.data
                $scope.showSheet("gckTypeObjA")
            })

        };

        $scope.choosePeople = function() {
            relModuleObj = {
                name: "FinanceFGckCtrl",
                selectTab: "project_money_select",
                selectTabContent: "工程名称"
            };
            // 清掉关联的编号
            $scope.param.taxReceiptNo = "";
            params = $scope.param;
            Util.savaParamForReview(params, $routeParams.id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        }

        $scope.chooseTax = function(type) {
            if ($scope.param.hasInvoice !== "已提供") {
                Util.Toast("请选择缴税发票")
                return;
            }
            if (!$scope.param.projectName) {
                Util.Toast("请选择工程名称")
                return;
            }
            relModuleObj = {
                name: "FinanceFGckCtrl",
                selectTab: "project_money_select_tax",
                propertyName: type,
                param: {
                    filterSql: $scope.param.contractNo,
                },
                selectTabContent: "缴税发票编号"
            };
            params = $scope.param;
            Util.savaParamForReview(params, $routeParams.id);
            Util.savaDataforsearch(relModuleObj);
            $location.path("finance/choosepeople")

        };
        $scope.submit = function() {
            // 底部类型end
            // $scope.param.tentativeCost = "11";
            if (!$scope.param.projectName) {
                Util.Toast("请填写工程名称");
                return;
            }
            if (!$scope.param.payType1) {
                Util.Toast("请填写工程款类型(大类)");
                return;
            }
            if (!$scope.param.payType2) {
                Util.Toast("请填写工程款类型(小类)");
                return;
            }
            if ($scope.param.projectName && $scope.param.hasInvoice == "已提供") {
                if (!($scope.param.taxReceiptNo || $scope.param.taxReceiptNo2 || $scope.param.taxReceiptNo3)) {
                    Util.Toast("请选择缴税发票编号");
                    return;
                }
            }
            if (!$scope.param.amount) {
                if ($scope.param.amount !== 0) {
                    Util.Toast("请填写付款金额");
                    return;
                }
            }
            if (!$scope.param.moneyRes) {
                Util.Toast("请填写款项来源");
                return;
            }
            if (!$scope.param.acceptCompany) {
                Util.Toast("请填写收款人");
                return;
            }
            if (!$scope.param.acceptAccount) {
                Util.Toast("请填写收款人帐号");
                return;
            }
            if (!$scope.param.acceptBank) {
                Util.Toast("请填写收款人开户行");
                return;
            }
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.projectMoney;
            var options = {};
            options.urlParam = urlParam;
            options.param = $scope.param;
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.projectMoney
            }
            new FormSubmit(options);

        };
        // 初始化逻辑,如果是修改页面的话则先从接口请求数据回显，再将缓存中的跳转来源删除，方便页面跳转返回之后的回显
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
            });
            // 显示审批记录判断
            Util.showloglist($routeParams.id, function(data) {
                $scope.logData = data;
            });
            $scope.disableInput = !!($scope.param && $scope.param.form_id)
        };
        // 初始化
        init();
    });
